﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{

    class Program
    {
        enum Fist
        {
            石头 = 1,
            剪刀,
            布
        }
        static string userName;
        static int x = 0;
        static int y = 0;
        static int j = 0;
        static int sum =0;
        static void Main(string[] args)
        {
            Console.WriteLine("------------欢 迎 进 入 游 戏 世 界------------");
            Console.WriteLine();
            Console.WriteLine("************************");
            Console.WriteLine("*******猜拳·开始*******");
            Console.WriteLine("************************");
            Console.WriteLine("出拳规则：{0}", ListFist());
            Task1();
        }

        public static void Task1()
        {
            x = 0;y = 0;j = 0;sum = 0;
            Console.WriteLine("请选择对方角色（1：刘备 2：孙权 3：曹操）");
            int Key = int.Parse(Console.ReadLine());
            Console.Write("请输入您的姓名：");
            userName = Console.ReadLine();
            string npcName = null;
            switch (Key)
            {
                case 1:
                    npcName = "刘备";
                    Go(userName, npcName);

                    break;
                case 2:
                    npcName = "孙权";
                    Go(userName, npcName);
                    break;
                case 3:
                    npcName = "曹操";
                    Go(userName, npcName);
                    break;
                default:
                    Console.WriteLine("选择出错！请重新选择！！");
                    Task1();
                    break;
            }
        }

        private static void Go(string a, string b)
        {
            NPC nPC = new NPC(a, b);
            User user = new User(a, b);

            Console.WriteLine($"{user.name} VS {nPC.name1} 对战");
            Console.WriteLine("开始游戏吗（y/n）");
            char key = char.Parse(Console.ReadLine());
            if (key == ('y' | 'Y'))
            {
                user.talk1();
                nPC.talk4();
                int f = (int)user.Fist();
                int i = (int)nPC.Fist();
                judge(f, i, nPC);
                select(nPC, user);
            }
            else if (key == ('n' | 'N'))
            {
                pan(nPC, user);
                Environment.Exit(0);
                
            }
            else
            {
                pan(nPC, user);
                Console.WriteLine("选择出错！自动退出");
                Environment.Exit(0);
            }

        }


        private static void select(NPC nPC, User user)
        {
            Console.WriteLine("是否开始下一轮（y/n）");
            char key = char.Parse(Console.ReadLine());
            if (key == ('y' | 'Y'))
            {
                user.talk1();
                nPC.talk4();
                int f = (int)user.Fist();
                int i = (int)nPC.Fist();
                judge(f, i, nPC);
                select(nPC, user);
            }
            else if (key == ('n' | 'N'))
            {
                pan(nPC, user);
                Environment.Exit(0);
            }
            else
            {
                Console.WriteLine("选择出错！自动退出");
                Environment.Exit(0);
            }
        }

        static void pan(NPC nPC, User user) 
        {
            Console.WriteLine("\n\n=======================================");
            Console.WriteLine($"{user.name} VS {nPC.name1}");
            Console.WriteLine("对战次数：{0}\n",sum = x + y + j);
            Console.WriteLine("姓名    得分");
            Console.WriteLine($"{user.name}   {y}");
            Console.WriteLine($"{nPC.name1}   {j}");
            Console.Write("结果：");
            if (y > j)
            {
                Console.WriteLine($"{user.name}赢,{nPC.name1}输。");
            }
            else if (y==j) 
            {
                Console.WriteLine("平局");
            }
            else
            {
                Console.WriteLine($"{nPC.name1}赢,{user.name}输。");
            }
            Console.WriteLine("是否开始下一局（y/n）");
            char key = char.Parse(Console.ReadLine());
            if (key == ('y' | 'Y'))
            {
                Task1();
            }
            else if (key == ('n' | 'N'))
            {
                Environment.Exit(0);
            }
            else
            {
                Console.WriteLine("选择出错！自动退出");
                Environment.Exit(0);
            }

        }
        static void judge(int f,int i,NPC nPC)
        {
            if (i==f)
            {
                x = x+1;
                nPC.talk3();
            }
            else if((f == 1 && i == 2) || (f == 2 && i ==3)|| (f == 3 && i == 1))
            {
                y = y +1;
                nPC.talk1();
            }
            else
            {
                j = j + 1;
                nPC.talk2();
            }
        }
        
        static string ListFist()
        {
            string fistString = null;
            int i = 1;
            foreach (Fist t in Enum.GetValues(typeof(Fist)))
            {
                fistString += $"{i}、{t} ";
                i++;
            }
            return fistString;
        }
        
    }
}
